package com.tacitknowledge.filters;

import com.tacitknowledge.filters.ClasspathFileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class GenericFilter implements Filter {
	public static final String CONFIG_FILE = "tk-filters.properties";
	private ClasspathFileReader configReader = new ClasspathFileReader();
	private Properties config = new Properties();
	private FilterConfig filterConfig;

	public GenericFilter() {
		this.configReader.setReloadSeconds(10);
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		this.doFilterInternal((HttpServletRequest) request, (HttpServletResponse) response, chain);
	}

	public abstract void doFilterInternal(HttpServletRequest arg0, HttpServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException;

	public abstract void printBanner(ServletContext arg0);

	public FilterConfig getFilterConfig() {
		return this.filterConfig;
	}

	public void setFilterConfig(FilterConfig val) {
		this.init(val);
	}

	public void init(FilterConfig val) {
		this.filterConfig = val;
		this.printBanner(val.getServletContext());
	}

	public void destroy() {
		this.filterConfig = null;
	}

	protected String getConfigEntry(String entry) {
		if (this.configReader.isModified("tk-filters.properties")) {
			InputStream is = this.configReader.getResourceAsStream("tk-filters.properties");
			if (is == null) {
				this.getFilterConfig().getServletContext()
						.log("[ERROR] Could not locate the filter config file: tk-filters.properties");
			} else {
				try {
					this.config.clear();
					this.config.load(is);
				} catch (IOException arg12) {
					this.getFilterConfig().getServletContext().log(
							"[ERROR] There was a problem reading the filter config file: tk-filters.properties", arg12);
				} finally {
					try {
						is.close();
					} catch (IOException arg11) {
						this.getFilterConfig().getServletContext().log("[ERROR] Unable to close config file", arg11);
					}

				}
			}
		}

		return this.config.getProperty(entry, "");
	}

	protected boolean getBooleanConfigEntry(String entry) {
		return "true".equals(this.getConfigEntry(entry));
	}

	protected int getIntConfigEntry(String entry, int defaultValue) {
		String s = this.getConfigEntry(entry);
		if (s.length() > 0) {
			try {
				return Integer.parseInt(s);
			} catch (NumberFormatException arg4) {
				this.getFilterConfig().getServletContext().log("Could not parse config attribute " + entry, arg4);
			}
		}

		return defaultValue;
	}

	protected boolean headerContains(HttpServletRequest request, String header, String value) {
		Enumeration<String> accepted = request.getHeaders(header);

		String headerValue;
		do {
			if (!accepted.hasMoreElements()) {
				return false;
			}

			headerValue = (String) accepted.nextElement();
		} while (headerValue.indexOf(value) == -1);

		return true;
	}

	protected boolean isEnabled() {
		return this.getBooleanConfigEntry(this.getFilterConfig().getFilterName() + ".Enabled");
	}

	public ClasspathFileReader getConfigReader() {
		return this.configReader;
	}

	public void setConfigReader(ClasspathFileReader configReader) {
		this.configReader = configReader;
	}
}